package client.control;

import base.BaseValue;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.DataFormatException;

/**
  * @Description 拍卖有关的网络接口
  * @author 孔明浩
  * @Date 2020/4/8
  */
public class AuctionControl {
    AuctionControl(){}
    public ResponseEntity addAuction(String token, int goodId, Date start, Date end, double maxPrice){
        ConnectControl connectControl = new ConnectControl();
        Map<String, String> map = new HashMap<>();
        map.put("goodId", goodId + "");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        map.put("start", simpleDateFormat.format(start));
        map.put("end", simpleDateFormat.format(end));
        map.put("maxPrice", maxPrice + "");
        connectControl.doPost(BaseValue.load + "addAuction", token, map);
        return ResponseEntity.fit(connectControl);
    }
    public ResponseEntity addAuction(String token,int goodId, String start, String end, double maxPrice){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            return addAuction(token,goodId,
                    simpleDateFormat.parse(start + " 00:00:00"),
                    simpleDateFormat.parse(end + " 23:59:59"),
                    maxPrice);
        } catch (ParseException e){
            e.printStackTrace();
        }
        return new ResponseEntity();
    }

    public ResponseEntity getAuctionInfo(int auctionId) {
        ConnectControl connectControl = new ConnectControl();
        Map<String, String> map = new HashMap<>();
        map.put("auctionId", auctionId + "");
        connectControl.doPost(BaseValue.load + "getAuctionInfo", null, map);
        return ResponseEntity.fit(connectControl);
    }

    public ResponseEntity bid(String token, int auctionId, double maxPrice){
        ConnectControl connectControl = new ConnectControl();
        Map<String, String> map = new HashMap<>();
        map.put("auctionId", auctionId + "");
        map.put("maxPrice", maxPrice + "");
        connectControl.doPost(BaseValue.load + "bid", token, map);
        return ResponseEntity.fit(connectControl);
    }
}
